﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Prompt.cs" company="Microsoft">
//   Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Activities;
using System.ComponentModel;
using System.Windows;
using SLS.ExClassLib.Message;
using SLS.ExClassLib.MVVM;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.WCFClient;
using SLS.ExClassLib.RTIService;
namespace SLS.ExClassLib.WFActivities
{ 
    /// <summary>
    /// The display prompt.
    /// </summary>
    public sealed class SendMessage : CodeActivity
    {
        #region Properties 
        /// <summary>
        ///   Gets or sets Text.
        /// </summary>
        [DefaultValue("NO")]
        public InArgument<string> JobID { get; set; }
        [DefaultValue("NO")]
        public InArgument<string> JobContent { get; set; }
        [DefaultValue("NO")]
        public InArgument<string> Tag { get; set; }
        [DefaultValue("WF2UI")]
        public InArgument<string> MessageType { get; set; }
        [DefaultValue("NO")]
        public InArgument<string> UpdateFedTime { get; set; }
        #endregion

        #region Methods

        /// <summary>
        /// The cache metadata.
        /// </summary>
        /// <param name="metadata">
        /// The metadata.
        /// </param>
        protected override void CacheMetadata(CodeActivityMetadata metadata)
        {
            metadata.RequireExtension(typeof(IDataModel));
            metadata.AddArgument(new RuntimeArgument("JobID", typeof(string), ArgumentDirection.In, false));
            metadata.AddArgument(new RuntimeArgument("JobContent", typeof(string), ArgumentDirection.In, false));
            metadata.AddArgument(new RuntimeArgument("MessageType", typeof(string), ArgumentDirection.In, false));
            metadata.AddArgument(new RuntimeArgument("Tag", typeof(string), ArgumentDirection.In, false));
            metadata.AddArgument(new RuntimeArgument("UpdateFedTime", typeof(string), ArgumentDirection.In, false));
        }

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        protected override void Execute(CodeActivityContext context)
        {
            try
            {
                // Obtain the runtime value of the Text input argument
                var wfvw= context.GetExtension<IDataModel>();
                var wfm = context.GetExtension<WFMachine>();
                MessageEx msg = new MessageEx(wfm.HostAddress, "WFFederation", this.JobID.Get(context), this.UpdateFedTime.Get(context));
                msg.JobType = this.MessageType.Get(context);
                msg.JobContent = this.JobContent.Get(context);
                if (this.Tag != null)
                {
                    msg.Tag = this.Tag.Get(context);
                }
                msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                //wfmViewModel.SendMessage(msg); 
                //wfm.MQ.Enqueue(msg);
                if (Application.Current != null)
                {
                    wfvw.SendMessage(msg); 
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        #endregion
    }
}